package com.starsoft.smdc.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.core.web.WebUtil;
import com.starsoft.smdc.bean.SmdcHolidayDetail;
import com.starsoft.smdc.service.HolidayService;

@RestController
@RequestMapping(value = "/smdc/calendar")
public class CalendarController {

	@Autowired
	HolidayService holidayService;
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/page")
	public Map<String, Object> pageQuery(HttpServletRequest request){
		Map<String, Object> model = new HashMap<String, Object>();
		PageObject po = WebUtil.getPageObject(request);
		model.putAll(holidayService.pageQuery(po));
		List<SmdcHolidayDetail> details = po.getRows();
		List<Map<String, Object>> detailMaps = new ArrayList<>();
		for(SmdcHolidayDetail detail: details){
			Map<String, Object> detailMap = new HashMap<String, Object>();
			detailMap.put("id", detail.getId());
			detailMap.put("code", detail.getSmdcHoliday().getCode());
			detailMap.put("specialDay", detail.getSpecialDay());
			detailMap.put("isHoliday", detail.getIsHoliday());
			detailMaps.add(detailMap);
		}
		po.setRows(detailMaps);
		model.putAll(po.getModel());
		return model;
	}
	
	@RequestMapping(value="/update")
	public Map<String, Object> update(HttpServletRequest request, SmdcHolidayDetail holidayDetail){
		Date date = holidayDetail.getSpecialDay();
		boolean isHoliday = holidayDetail.getIsHoliday();
		holidayService.insertHoliday(date, isHoliday);
		return WebUtil.sucesseModel(null);
	}
	
	@RequestMapping(value="/delete")
	public Map<String, Object> delete(HttpServletRequest request, SmdcHolidayDetail holidayDetail){
		holidayService.delete(holidayDetail);
		return WebUtil.sucesseModel("删除成功");
	}
	
}
